from flask import Blueprint, request, jsonify
from algorithm.analyze_syntax import analyze_syntax  #句型分析函数
from algorithm.analyze_syntax import analyze_syntax_tree #句子依存关系函数
from algorithm.analyze_syntax import analyze_syntax_structure # 语法结构可视化数据生成函数

# 注册蓝图时添加前缀
essayBp = Blueprint("essay", __name__)

@essayBp.route('/analyze_syntax', methods=['POST'])
def analyze_syntax_route():
    data = request.get_json()
    essay_text = data.get('essayText')

    if not essay_text:
        return jsonify({"error": "作文内容不能为空"}), 400

    try:
        analysis_result = analyze_syntax(essay_text)
        return jsonify({"result": analysis_result}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@essayBp.route('/analyze_syntax_tree', methods=['POST'])
def analyze_syntax_tree_route():
    data = request.get_json()
    sentence = data.get('sentence')

    if not sentence:
        return jsonify({"error": "句子内容不能为空"}), 400

    try:
        tree_data = analyze_syntax_tree(sentence)
        return jsonify(tree_data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@essayBp.route('/analyze_syntax_structure', methods=['POST'])
def analyze_syntax_structure_route():
    data = request.get_json()
    sentence = data.get('sentence')

    if not sentence:
        return jsonify({"error": "句子内容不能为空"}), 400

    try:
        structure_data = analyze_syntax_structure(sentence)
        return jsonify(structure_data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


